<template>
	<view class="off-campus">
		<u-sticky bgColor="#fff">
			<Header></Header>
			<u-tabs :list="eventTabs" @click="handleChangeTab"></u-tabs>
		</u-sticky>
		<view class="list" v-if="list.length">
			<ActivityBox :eventList="list" :eventType="eventTabIndex"></ActivityBox>
			<u-loadmore :status="loadmoreStatus" :marginTop="30" :marginBottom="30" line />
		</view>
		<view class="empty" v-else>
			<u-empty mode="search" :iconSize="120" text="研学项目为空"></u-empty>
		</view>
	</view>
</template>

<script>
	import Header from '@/components/header.vue';
	import ActivityBox from '@/components/activityBox.vue';

	export default {
		components: {
			Header,
			ActivityBox,
		},
		data() {
			return {
				eventTabIndex: 0,
				eventTabs: [{
					name: '校内必选'
				}, {
					name: '校外精选'
				}],
				page: 1,
				size: 10,
				list: [],
				loadmoreStatus: 'loadmore',
			}
		},
		onLoad() {
			this.getList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.list = [];
			this.getList();
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if (this.loadmoreStatus === 'nomore') return;
			this.loadmoreStatus = 'loading';
			this.page += 1;
			this.getList();
		},
		methods: {
			handleChangeTab(e) {
				this.eventTabIndex = e.index;
				this.page = 1;
				this.list = [];
				this.loadmoreStatus = 'loadmore';
				this.getList();
			},

			async getList() {
				const res = await this.$api('/study/mobile/event/page', 'get', {
					page: this.page,
					limit: this.size,
					type: this.eventTabIndex === 1 ? 'OFF_CAMPUS' : 'ON_CAMPUS'
				});

				this.loadmoreStatus = 'loadmore';
				this.list = this.list.concat(res.data.list);
				
				console.log(this.list);

				if (res.data.list.length < this.size) {
					this.loadmoreStatus = 'nomore';
					return;
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.off-campus {
		margin: 20rpx;

		.list {}
	}
</style>